/**
 * 风险预警看板
 */
odoo.define('lift_risk.risk_warning', function (require) {
    "use strict";
    var ajax = require('web.ajax');
    var core = require('web.core');
    var Widget = require('web.Widget');
    var Model = require('web.Model');
    var session = require('web.session');
    var framework = require('web.framework');
    var form_common = require('web.form_common')
    var list_widget_registry = core.list_widget_registry;
    var ListView = require("web.ListView");
    var _t = core._t;

    //维保项的消息盒子
    var MiantenanceBoxWidget = Widget.extend({
        template:"lift_risk_warning_box_template",
        events:{
            "click a.tyibs-risk-warning-box-button":"call_window_action"
        },
        init:function(parent, title, count, bg_color, fa_icon, res_model, domain){
            this._super(parent, arguments);
            this.title = title;
            this.count = count;
            this.bg_color = bg_color;
            this.fa_icon = fa_icon;
            this.res_model = res_model;
            this.domain = domain || [['id','!=',0]];
        },
        call_window_action:function(){
            var self = this;
            if(self.res_model==undefined){
                return false;
            }
            self.do_action({
                name: self.title,
                type: 'ir.actions.act_window',
                res_model: self.res_model,
                views: [[false, 'list'],[false, 'form']],
                target: 'current',
                domain: self.domain,
                context: {},
            });
            return false;
        }
    });

    //风险统计页面的统计盒子
    var LiftRiskWarningWidget = Widget.extend({
        template: "lift_risk_warning_index",
        start:function(){
            this.show_maintenance_box_list();
            this.show_save_box_list();
            this.show_check_box_list();
        },
        show_maintenance_box_list:function(){
            var _this = this;
            var nowTime = new Date();
            var last_30_days = moment().add(-30,'days').format("YYYY-MM-DD");
            var main_container = this.$("div.tyibs-risk-warning-box-container");
            // 超期维保
            new Model("tyibs.main.work.order").query(['id']).filter([['over_due_state','=','over_due_has_main']]).count()
            .then(function(result){
                new MiantenanceBoxWidget(_this, "超期维保", result, "bg-red", "fa-subway", "tyibs.main.work.order", [['over_due_state','=','over_due_has_main']])
                .appendTo(main_container);
            })
            // 超期未维保（这是系统自动生成出来的）
            new Model("tyibs.main.work.order").query(['id']).filter([['over_due_state','=','over_due_not_main']]).count()
            .then(function(result){
                new MiantenanceBoxWidget(_this, "超期未维保", result, "bg-red", "fa-subway", "tyibs.main.work.order", [['over_due_state','=','over_due_not_main']])
                .appendTo(main_container);
            })
            // 脱保电梯
            new Model("tyibs.base.lift").query(['id']).filter([['maintenance_company_id','=',false],['lift_status','=','using']]).count()
            .then(function(result){
                new MiantenanceBoxWidget(_this, "脱保电梯", result, "bg-red", "fa-subway", "tyibs.base.lift", [['maintenance_company_id','=',false],['lift_status','=','using']])
                .appendTo(main_container);
            })
            // 维保人员证书到期:维保人员有多个证书，有一个过期就过期，这个需要特殊处理：zrtodo
            new Model("tyibs.base.maintenance.company.employee.certificate").query(['id']).filter([['has_over_time','=',true]]).count()
            .then(function(result){
                new MiantenanceBoxWidget(_this, "维保人员证书到期", result, "bg-red", "fa-wrench","tyibs.base.maintenance.company.employee.certificate",[['has_over_time','=',true]])
                .appendTo(main_container);
            })
            // 超期未注册:超过检验时间30天还没有注册代码的:没有检验时间的电梯不统计
            new Model("tyibs.base.lift").query(['id']).filter([['last_check_date','<',last_30_days],['register_code','=',false]]).count()
            .then(function(result){
                new MiantenanceBoxWidget(_this, "超期未注册", result, "bg-red", "fa-subway", "tyibs.base.lift", [['last_check_date','<',last_30_days],['register_code','=',false]])
                .appendTo(main_container);
            })
            // 维保单位资质超期：特种设备安装维修许可证信息：有效期：没有不统计（来源维保单位备案）
            new Model("tyibs.base.maintenance.company").query(['id']).filter([['special_card_period','<',nowTime]]).count()
            .then(function(result){
                new MiantenanceBoxWidget(_this, "维保单位资质超期", result, "bg-red", "fa-wrench","tyibs.base.maintenance.company",[['special_card_period','<',nowTime]])
                .appendTo(main_container)
            })
            // 维保单位备案超期：本地4年异地1年，这里以下次备案日期为准，1是为了能整合本地异地，2是防止备案时间间隔若有修改不必修改此处代码
            new Model("tyibs.base.maintenance.company").query(['id']).filter([['next_filing_date','<',nowTime]]).count()
            .then(function(result){
                new MiantenanceBoxWidget(_this, "维保单位备案超期", result, "bg-red", "fa-wrench","tyibs.base.maintenance.company",[['next_filing_date','<',nowTime]])
                .appendTo(main_container)
            })
            // ----------以下是未实现的----------------
            // zrtodo:不知道未来要不要做
            // new MiantenanceBoxWidget(this, "超负荷维保", 132, "bg-red", "fa-subway").appendTo(main_container);
        },
        show_save_box_list:function(){
            var _this = this;
            var save_container = this.$("div.tyibs-risk-warning-save-box-container");
            var today = new Date();
            var month_start = moment().format('YYYY-MM-01');
            var month_end = moment().add(1,"months").format("YYYY-MM-01");
            var week_start = moment().startOf('week').format("YYYY-MM-DD");
            var week_end = moment().endOf('week').format("YYYY-MM-DD");
            var today_start = moment().format("YYYY-MM-DD");
            var today_end = moment().add(1,'days').format("YYYY-MM-DD");
            // 全部接警
            new Model("tyibs.er.work.order.save").query(['id']).filter([]).count()
            .then(function(result){
                new MiantenanceBoxWidget(_this, "全部接警", result,"bg-green", "fa-ambulance","tyibs.er.work.order.save", false)
                .appendTo(save_container);
            })
            // 本月接警
            new Model("tyibs.er.work.order.save").query(['id']).filter([['service_time','<',month_end],['service_time','>=',month_start]]).count()
            .then(function(result){
                new MiantenanceBoxWidget(_this, "本月接警", result,"bg-green", "fa-ambulance","tyibs.er.work.order.save", [['service_time','<',month_end],['service_time','>=',month_start]])
                .appendTo(save_container);
            })
            // 本周接警
            new Model("tyibs.er.work.order.save").query(['id']).filter([['service_time','<=',week_end],['service_time','>=',week_start]]).count()
            .then(function(result){
                new MiantenanceBoxWidget(_this, "本周接警", result,"bg-green", "fa-ambulance","tyibs.er.work.order.save", [['service_time','<=',week_end],['service_time','>=',week_start]])
                .appendTo(save_container);
            })
            // 今天接警
            new Model("tyibs.er.work.order.save").query(['id']).filter([['service_time','<=',today_end],['service_time','>=',today_start]]).count()
            .then(function(result){
                new MiantenanceBoxWidget(_this, "今天接警", result,"bg-green", "fa-ambulance","tyibs.er.work.order.save", [['service_time','<=',today_end],['service_time','>=',today_start]])
                .appendTo(save_container);
            })
        },
        show_check_box_list:function(){
            var _this = this;
            var today = new Date();
            var check_container = this.$("div.tyibs-risk-warning-check-box-container");
            // 整改期间电梯
            var corrective_domain = [['lift_status','=','using'],['checkout_result','=','corrective']];
            new Model("tyibs.base.lift").query(['id']).filter(corrective_domain).count()
            .then(function(result){
                new MiantenanceBoxWidget(_this, "整改期间", result,"bg-yellow", "fa-check-circle-o","tyibs.base.lift", corrective_domain)
                .appendTo(check_container);
            });
            // 检验不合格电梯
            var check_fail_domain = [['lift_status','=','using'],'|',['checkout_result','=','no'],['checkout_result','=','re_no']];
            new Model("tyibs.base.lift").query(['id']).filter(check_fail_domain).count()
            .then(function(result){
                new MiantenanceBoxWidget(_this, "检验不合格", result,"bg-red", "fa-check-circle-o","tyibs.base.lift", check_fail_domain)
                .appendTo(check_container);
            });
            // 未经检验使用：超过检验日期：没有检验日期的不统计
            // 2018-09-04 超期未检去除整改期间的电梯
            var not_check_domain = [['lift_status','=','using'],['next_check_date','<',today],['checkout_result','!=','corrective']];
            new Model("tyibs.base.lift").query(['id']).filter(not_check_domain).count()
            .then(function(result){
                new MiantenanceBoxWidget(_this, "未经检验使用", result,"bg-red", "fa-check-circle-o","tyibs.base.lift", not_check_domain)
                .appendTo(check_container);
            })
        }
    });

    core.action_registry.add('lift_risk.risk_warning_index', LiftRiskWarningWidget);
});